<template>
  <div class style="height: 100%; padding: 20px">
    <div :id="uniqueID" style="height: 100%; width: 100%"></div>
  </div>
</template>
<script>
import * as echarts from "echarts/dist/echarts";
import uuidv1 from "uuid/v1";
import commonmixins from "@/mixins/bigScreen/commonmixins";
import axios from "@/utils/requestdp";

import dayjs from "dayjs";

export default {
  name: "commontianqi7day",
  mixins: [commonmixins],
  components: {},
  data() {
    return {
      uniqueID: "",
    };
  },
  computed: {},
  watch: {
    "$store.state.daping.commontianqilishi": {
      handler: function (n, o) {
        this.loadData(n);
      },
    },
  },
  created() {
    this.uniqueID = uuidv1();
  },
  mounted() {
    // this.loadData();
  },
  methods: {
    async loadData(res) {
      let scale = this.$root.setting.scale * this.scalecom;

      var dayForecastData_day = [];
      var dayForecastData_night = [];
      var dayForecastData_avg = [];
      var dayForecastData = [];

      let dayForecastDataArr = [];

      if (res.code == 200) {
        //返回成功

        res.data.weather.map((item) => {
          var dayTemp = Number(item.day_air_temperature);
          var nightTemp = Number(item.night_air_temperature);

          let date_arr = item["date"].split("-");
          let date = date_arr[1] + "-" + date_arr[2];

          dayForecastData_day.push([date, dayTemp]);
          dayForecastData_night.push([date, nightTemp]);
          dayForecastData_avg.push([
            date,
            (Number(dayTemp + nightTemp) / 2).toFixed(0),
          ]);
        });

        dayForecastDataArr = [
          dayForecastData_night,
          dayForecastData_day,
          dayForecastData_avg,
        ];

        var Line = ["最低温度", "最高温度", "平均温度"];
        var color = ["#0696F9", "#DDF776", "#53F3F3"];
        var img = [
          "image://",
          "image://",
          "image://",
        ];

        //数据处理
        Line.map((item, index) => {
          dayForecastData.push({
            symbolSize: 150,
            symbol: img[index],
            name: item,
            type: "line",
            // yAxisIndex: 1,
            data: dayForecastDataArr[index],
            itemStyle: {
              normal: {
                borderWidth: 5,
                color: color[index],
              },
            },
          });
        });
      }

      var weekOption = {
        // backgroundColor: '#0e2147',
        // title: {
        //   text: "未来七天天气预报(℃)",
        //   textStyle: {
        //     color: "#fff",
        //     fontSize: 36 * scale,
        //     fontWeight: "bold"
        //   }
        // },
        tooltip: {
          show: true,
          trigger: "axis",
          // formatter: "{a} {b}: {c} ℃",
          textStyle: {
            fontWeight: "normal",
            fontSize: 30 * scale,
          },
        },
        grid: {
          left: "5%",
          right: "5%",
          bottom: "5%",
          top: "20%",
          containLabel: true,
        },
        legend: {
          type: "scroll",
          // data: Line,
          // itemWidth: 18,
          // ite mHeight: 12,
          right: "0%",
          textStyle: {
            color: "#F5F5F5",
            fontSize: 28 * scale,
            // padding: [15, 0, 0, 0],
          },
        },
        yAxis: [
          {
            type: "value",
            // name:'单位:℃',
            // position: 'left',
            nameTextStyle: {
              color: "#F5F5F5",
            },
            axisLabel: {
              formatter: "{value}",
              color: "#e2e9ff",
              textStyle: {
                color: "#ffffff",
                fontSize: 30 * scale,
              },
            },
            axisLine: {
              show: false,
            },
            splitLine: {
              lineStyle: {
                color: "rgba(255,255,255,0.12)",
              },
            },
          },
        ],
        xAxis: [
          {
            type: "category",
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: "#6A989E",
              },
            },
            axisLabel: {
              inside: false,
              interval: 0, //【2019年11月6日】需要全部显示出来 线上的点也才会都在
              textStyle: {
                color: "#fff", // x轴颜色
                fontWeight: "normal",
                fontSize: 30 * scale,
                // lineHeight: 22
              },
              // formatter: function (value, index) {
              //     var year = value.substring(0,4);//不包含最后一个字符
              //     var month = value.substring(4,6);
              //     var day = value.substring(6,8);
              //     var hour = value.substring(8,10);
              //     var minute = value.substring(10,12);

              //     return year+"-"+month+"-"+day;
              // }
            },
          },
        ],
        series: dayForecastData,
      };

      var weatherEchartWeek = echarts.init(
        document.getElementById(this.uniqueID)
      );
      weatherEchartWeek.setOption(weekOption);

      window.addEventListener("resize", () => {
        weatherEchartWeek.resize();
      });
    },
  },
};
</script>
<style lang='scss' scoped>
</style>